package com.aube.rvi;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.view.ViewGroup;

import com.aube.rvi.module.DLLayout;
import com.aube.rvi.module.DLPluginManager;
import com.aube.rvi.module.DLPluginPackage;
import com.aube.rvi.module.ModuleManager;
import com.aube.rvi.statistic.AubeDatabaseHelper;
import com.aube.rvi.util.AppUtil;
import com.aube.rvi.util.Constant;
import com.aube.rvi.util.StorageUtil;

/**
 * Created by huyaonan on 16/10/20.
 */
public class AubeAgent {

    public static final String SDK_VERSION = "v0.1.0";

    public static void init(Context context, String appKey, String privateKey) {
        StorageUtil.initDiskCacheDir(context);

        Constant.APP_KEY = appKey;
        Constant.PRIVATE_KEY = privateKey;

        AubeDatabaseHelper.getInstance(context).upload();

        //fill Constant Parameters
        initAppArgs(context);
    }

    public static void report(Context context) {
        AubeDatabaseHelper.getInstance(context).upload();
    }

    public static void report(Context context, String key, String value) {
        AubeDatabaseHelper.getInstance(context).report(key, value);
    }

    private static void initAppArgs(Context context) {
        try {
            Constant.APP_MOBILETYPE = Build.MODEL;
            Constant.APP_DEVICEID = AppUtil.getDeviceId(context);

            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            Constant.APP_NAME = info.packageName;

            //会因为权限问题导致异常,放在最后处理,以防影响其它的值的获取
            Constant.APP_IMEI = AppUtil.getIMEI(context);// 获取IMEI号
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(final Context context, final ViewGroup viewGroup, final String videoId, final OnAubePreparedListener listener) {
        ModuleManager.getInstance(context).getModules(videoId, new OnModulePreparedListener() {
            @Override
            public void onReady(DLPluginPackage pluginPackage, String packageName) {
                try {
                    if (listener != null)
                        listener.onPrepared(load(context, viewGroup, videoId, pluginPackage, packageName));
                } catch (Exception e) {
                    e.printStackTrace();
                    if (listener != null)
                        listener.onError(e);
                }
            }

            @Override
            public void onFail() {
                if(listener != null)
                    listener.onError(new Exception("module download fail"));
            }
        });
    }

    private static OnVideoPlayerListener load(Context context, ViewGroup viewGroup, String videoId, DLPluginPackage pluginPackage, String packageName) {
        DLLayout layout = ModuleManager.getInstance(context).inflatePluginLayout(new ProxyContext(context, pluginPackage), pluginPackage, packageName + ".AubeInteractionLayout");
        viewGroup.addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.fillData(videoId);
        return layout;
    }

    public static OnVideoPlayerListener load(Context context, ViewGroup viewGroup, String videoId) {
        DLPluginPackage pluginPackage = installPluginFromSDCard(context);
        DLLayout layout = ModuleManager.getInstance(context).inflatePluginLayout(new ProxyContext(context, pluginPackage), pluginPackage, "com.aube.interact.AubeInteractionLayout");
        viewGroup.addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.fillData(videoId);
        return layout;
    }

    private static DLPluginPackage installPluginFromSDCard(Context context) {
        return DLPluginManager.getInstance(context).loadApk(Environment.getExternalStorageDirectory().getPath() + "/plugin_test.apk", "theme");
    }

}
